package com.lingtianyu.tech.erbaoshen.core.util.serialize;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class FastJsonSerializeExecute implements SerializeExecute {
    @Override
    public Object unSerializeArray(Object param,Type type) {
        Class<?> aClass = (Class<?>) type;
        JSONArray jsonArray = (JSONArray)JSONArray.parse(param.toString());
        List<?> objects = jsonArray.toJavaList(aClass.getComponentType());
        return objects.toArray();
    }

    @Override
    public String unSerializeString(Object param) {
        return param.toString();
    }

    @Override
    public Date unSerializeDate(Object param) {
        return JSON.parseObject(param.toString(), Date.class);
    }

    @Override
    public Boolean unSerializeBoolean(Object param) {
        return JSON.parseObject(param.toString(), boolean.class);
    }

    @Override
    public Character unSerializeChar(Object param) {
        return JSON.parseObject(param.toString(), char.class);
    }

    @Override
    public Byte unSerializeByte(Object param) {
        return JSON.parseObject(param.toString(), byte.class);
    }

    @Override
    public Short unSerializeShort(Object param) {
        return JSON.parseObject(param.toString(), short.class);
    }

    @Override
    public Integer unSerializeInt(Object param) {
        return JSON.parseObject(param.toString(), int.class);
    }

    @Override
    public Long unSerializeLong(Object param) {
        return JSON.parseObject(param.toString(), long.class);
    }

    @Override
    public Float unSerializeFloat(Object param) {
        return JSON.parseObject(param.toString(), float.class);
    }

    @Override
    public Double unSerializeDouble(Object param) {
        return JSON.parseObject(param.toString(), double.class);
    }

    @Override
    public BigDecimal unSerializeBigDecimal(Object param) {
        return JSON.parseObject(param.toString(), BigDecimal.class);
    }

    @Override
    public Object unSerializeObject(Object param, Type type) {
        return JSON.parseObject(param.toString(), type);
    }

    @Override
    public Object unSerializeDefault(Object param, Type type) {
        return JSON.parseObject(param.toString(), type);
    }

    @Override
    public String serializeParam(Object param) {
        return JSON.toJSONString(param);
    }

    @Override
    public String getType() {
        return "fastjson";
    }
}
